//
//  LOLimageViewController.m
//  LOLXXbox
//
//  Created by mac on 16/3/1.
//  Copyright © 2016年 Grasstrio. All rights reserved.
//

#import "LOLimageViewController.h"
#import "LOLBaseCollectionView.h"
#import "LSZNetWorkTools.h"
#import "LOLimagesModel.h"
#import "LOLimagesCollectionViewCell.h"
#define SKINURL(name)   [NSString stringWithFormat:@"http://box.dwstatic.com/apiHeroSkin.php?hero=%@",name]

#define WIDTH [[UIScreen mainScreen]bounds].size.width
#define HEIGHT [[UIScreen mainScreen]bounds].size.height
#define PADDING5 WIDTH/75

@interface LOLimageViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,retain)UIButton *backButton;
@property(nonatomic,retain)UILabel *detailLabel;
@property(nonatomic,retain)UILabel *priceLabel;
@property(nonatomic,retain)UIButton *saveButton;
@property(nonatomic,retain)UICollectionView *skinCollectionV;
@property(nonatomic,retain)NSMutableArray *skinArr;
@property(nonatomic,retain)UILabel *pageLabel;
@property(nonatomic,retain)UIView *topView;
@property(nonatomic,retain)UIView *bottomView;
@property(nonatomic,assign)BOOL isHidden;
@end

@implementation LOLimageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createCollectionV];
    [self createData];

}

-(BOOL)prefersStatusBarHidden
{
    
    return YES;
    
}

-(void)createCollectionV
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeHiddenType) name:@"hiddenView" object:nil];
    
    self.navigationController.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor blackColor];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.skinCollectionV = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowLayout];

    self.skinCollectionV.backgroundColor = [UIColor blackColor];
    [self.skinCollectionV registerClass:[LOLimagesCollectionViewCell class] forCellWithReuseIdentifier:@"skinReuse"];
    self.skinCollectionV.delegate = self;
    self.skinCollectionV.dataSource = self;
    self.skinCollectionV.pagingEnabled = YES;
    self.skinCollectionV.allowsSelection = NO;
    
    [self.view addSubview:self.skinCollectionV];
    
    
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/17)];
    [self.view addSubview:self.topView];

    
    self.bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT-HEIGHT/8, WIDTH, HEIGHT/8)];
    [self.view addSubview:self.bottomView];

    
    self.topView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.650];
    self.bottomView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.650];
    
    
    
    
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.backButton.frame = CGRectMake(5, 10, WIDTH/17, WIDTH/17);
    [self.backButton addTarget:self action:@selector(dismissVC:) forControlEvents:UIControlEventTouchUpInside];
    [self.backButton setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [self.topView addSubview:self.backButton];
    
    
    
    
    
    
    
    self.detailLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 0, WIDTH-10, 40)];
    self.detailLabel.font = [UIFont boldSystemFontOfSize:18];
    self.detailLabel.textColor = [UIColor whiteColor];
    [self.bottomView addSubview:self.detailLabel];

    
    self.priceLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH/2,0, WIDTH/2-5, 40)];
    self.priceLabel.font = [UIFont boldSystemFontOfSize:15];
    self.priceLabel.textAlignment = NSTextAlignmentRight;
    self.priceLabel.textColor = [UIColor whiteColor];
    [self.bottomView addSubview:self.priceLabel];

    
    
    self.pageLabel =[[UILabel alloc]initWithFrame:CGRectMake(0,45, WIDTH, 30)];
    self.pageLabel.textAlignment = NSTextAlignmentCenter;
    self.pageLabel.font = [UIFont boldSystemFontOfSize:16];
    self.pageLabel.textColor = [UIColor whiteColor];
    [self.bottomView addSubview:self.pageLabel];

    
    
    
    self.saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.saveButton.frame = CGRectMake(PADDING5, 45, WIDTH/15, WIDTH/15);
    [self.saveButton addTarget:self action:@selector(saveToPhoto:) forControlEvents:UIControlEventTouchUpInside];
    [self.saveButton setImage:[UIImage imageNamed:@"download"] forState:UIControlStateNormal];
    [self.bottomView addSubview:self.saveButton];
    
    
    
    
    
    
}

-(void)createData
{

    [LSZNetWorkTools getWithUrl:SKINURL(self.enName) parameter:nil httpHeader:nil resposeType:ResponseTypeJSON success:^(id result) {
        self.skinArr = [LOLimagesModel baseModelByArr:result];
                LOLimagesModel *model = self.skinArr[0];
                self.detailLabel.text = model.name;
                self.priceLabel.text =@"默认皮肤";
                self.pageLabel.text = [NSString stringWithFormat:@"1 of %ld",self.skinArr.count];
        
                [self.skinCollectionV reloadData];

        
    } fail:^(NSError *error) {
        
        
    }];
    
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.skinArr.count;
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LOLimagesCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"skinReuse" forIndexPath:indexPath];
    cell.IBModel = self.skinArr[indexPath.row];
    return  cell;
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

-(void)dismissVC:(UIButton *)button
{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    self.saveButton.userInteractionEnabled = YES;
    
    LOLimagesModel *model = self.skinArr[(NSInteger)(scrollView.contentOffset.x/WIDTH)];
    
    self.detailLabel.text = model.name;
    
    if(![model.price isEqualToString:@"未上架"]&&![model.price isEqualToString:@"0"])
    {
        self.priceLabel.text = [NSString stringWithFormat:@"%@点券",model.price];
    }
    else if([model.price isEqualToString:@"0"])
    {
        self.priceLabel.text =@"默认皮肤";
        
        
    }
    else
    {
        self.priceLabel.text = model.name;
    }
    self.pageLabel.text = [NSString stringWithFormat:@"%ld of %ld",(NSInteger)(scrollView.contentOffset.x/WIDTH)+1,self.skinArr.count];
    
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    self.saveButton.userInteractionEnabled = NO;
    
    
}

-(void)changeHiddenType
{
    if(self.isHidden)
    {
        self.topView.hidden =YES;
        self.bottomView.hidden = YES;
        
        
    }
    else
    {
        self.topView.hidden =NO;
        self.bottomView.hidden = NO;
        
    }
    
    self.isHidden = !self.isHidden;
    
    
    
    
}

-(void)saveToPhoto:(UIButton *)button
{
    
    LOLimagesCollectionViewCell *cell = self.skinCollectionV.visibleCells[0];
    UIImageWriteToSavedPhotosAlbum(cell.skinImage, nil, nil, nil);
    
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
